package com.jokerpan.project.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.PaginationBuilder;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.Comment;
import com.jokerpan.project.entity.Users;
import com.jokerpan.project.mapper.CommentMapper;
import com.jokerpan.project.service.CommentService;
import com.jokerpan.project.util.RecursiveListUtils;
import com.jokerpan.project.util.UserTool;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jokerpan
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {
    @Resource
    CommentMapper commentMapper;
    @Resource
    UserTool userTool;


    @Override
    public Map<String, Object> queryList(Comment comment) {
        Page<Object> page = PageHelper.startPage(comment.getCurrentPage(), comment.getPageSize());
        List<Comment> resultList = this.commentMapper.queryList(comment);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), comment.getCurrentPage(), comment.getPageSize());
    }

    @Override
    public List<LinkedHashMap<String, Object>> queryByBlogId(Comment comment) {
        List<LinkedHashMap<String, Object>> list = this.commentMapper.queryByBlogId(comment);
        return RecursiveListUtils.queryRecursiveList(list);
    }

    @Override
    public Boolean add(Comment comment){
        if (comment.getContent() == null || comment.getContent().equals("")){
            return false;
        }
        Users users = userTool.getCurrentUser();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        comment.setUserId(users.getId());
        comment.setCommentTime(format.format(new Date()));
        try {
            return commentMapper.add(comment) > 0;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public ActionResult update(Comment comment) {
        if (commentMapper.update(comment) > 0){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

    @Override
    public ActionResult like(Comment comment) {
        Comment obj = commentMapper.queryById(comment.getId());
        comment.setLikeCount((obj.getLikeCount() == null ? 0 : obj.getLikeCount()) + 1);
        if (commentMapper.like(comment) > 0){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed("操作失败！");
        }
    }

    @Override
    public ActionResult dislike(Comment comment) {
        Comment obj = commentMapper.queryById(comment.getId());
        comment.setDislikeCount((obj.getDislikeCount() == null ? 0 : obj.getDislikeCount()) + 1);
        if (commentMapper.dislike(comment) > 0){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed("操作失败！");
        }
    }

    @Override
    public ActionResult deleteById(Integer id) {
        if (commentMapper.deleteById(id) > 0){
            commentMapper.updateChildren(id);
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }
}
